package com.wzx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzx.common.exception.LeadNewsException;
import com.wzx.user.dto.BindingDto;
import com.wzx.user.mapper.BindingMapper;
import com.wzx.user.pojo.Binding;
import com.wzx.user.sdk.common.identity.java.IdentityUtils;
import com.wzx.user.service.BindingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author wzx
 * @version 1.0
 * @date 2023/3/11 - 14:36
 * @description 标题
 */
@Service
public class BindingServiceImpl extends ServiceImpl<BindingMapper, Binding> implements BindingService {

    @Autowired
    private BindingMapper bindingMapper;

    @Override
    public BindingDto queryIsBinding(Integer userId) {
        //查询用户是否有绑定数据
        QueryWrapper<Binding> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        Binding bindings = bindingMapper.selectOne(queryWrapper);

        BindingDto bindingDto = new BindingDto();
        bindingDto.setIsBinding(!StringUtils.isEmpty(bindings));
        if (!StringUtils.isEmpty(bindings)) {
            bindingDto.setPhone(bindings.getPhone());
        }
        return bindingDto;
    }

    @Override
    public Boolean queryBindingByIdCard(Binding binding) throws Exception {
        //查询同样用户是否绑定数量超过8
        QueryWrapper<Binding> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", binding.getName());
        queryWrapper.eq("idCard", binding.getIdCard());
        List<Binding> bindings = bindingMapper.selectList(queryWrapper);
        if (bindings.size() > 8) {
            throw new LeadNewsException("该身份已绑定多个用户！");
        }

        //判断实名认证
        Map<String, Object> stringObjectMap = IdentityUtils.identityUtils(binding.getName(), binding.getIdCard());
        if (Integer.parseInt(stringObjectMap.get("code").toString()) != 1) {
            throw new LeadNewsException(stringObjectMap.get("message").toString());
        }

        return true;
    }

    @Override
    public void secureBinding(Binding binding) throws LeadNewsException {
        QueryWrapper<Binding> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", binding.getName());
        queryWrapper.eq("phone", binding.getPhone());
        queryWrapper.eq("userId", binding.getUserId());
        Binding bindings = bindingMapper.selectOne(queryWrapper);
        if (StringUtils.isEmpty(bindings)) {
            throw new LeadNewsException("账号，姓名输入错误！");
        }
        //删除绑定
        bindingMapper.deleteById(bindings);
    }

    @Override
    public Boolean insertBinding(Binding binding) {
        QueryWrapper<Binding> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", binding.getName());
        queryWrapper.eq("phone", binding.getPhone());
        queryWrapper.eq("userId", binding.getUserId());
        Binding bindings = bindingMapper.selectOne(queryWrapper);
        if (bindings != null) {
            //删除之前绑定
            bindingMapper.deleteById(bindings.getId());
        }
        //再次绑定
        bindingMapper.insert(binding);
        return true;
    }
}
